package leetcode.双指针法;

import java.util.Arrays;

/**
 * 移动零
 */
public class Test283移动零 {


    public static void moveZeroes(int[] nums) {
        // 双指针法：慢指针永远指向 0，快指针找到一个非0的数后与他交换
        int s = 0;
        int f = 1;
        for (; f < nums.length; f++) {
            if (nums[f] != 0 && nums[s] == 0) {
                swap(nums, s, f);
                s++;
                continue;
            }
            if (nums[s] != 0) {
                s++;
            }
        }
    }

    public static void swap(int[] nums, int slow, int fast) {
        nums[slow] ^= nums[fast];
        nums[fast] ^= nums[slow];
        nums[slow] ^= nums[fast];
    }

    public static void main(String[] args) {
//        int[] array = new int[]{0,1,0,3,12};
//        int[] array = new int[]{1,0,1};
        int[] array = new int[]{1, 0};
//        int[] array = new int[]{0};
//        int[] array = new int[]{2,1};
        moveZeroes(array);
        System.out.println(Arrays.toString(array));
    }
}
